//
//  KSRegisterViewController.swift
//  Merchant
//
//  Created by liuxm on 2017/8/8.
//  Copyright © 2017年 huanniu. All rights reserved.
//

import UIKit

/// 注册
class KSRegisterViewController: KSBaseViewController {
    /// 手机
    @IBOutlet weak var phoneNumTf: UITextField!
    /// 真实姓名
    @IBOutlet weak var nameTf: UITextField!
    /// 推荐人
    @IBOutlet weak var refereeAccountTf: UITextField!
    /// 同意
    @IBOutlet weak var agreeBtn: UIButton!
    /// 下一步
    @IBOutlet weak var nextBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.title = "注册"
        
        self.nextBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorMainRed), for: UIControlState.normal)
        self.nextBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonHighlight), for: UIControlState.highlighted)
        self.nextBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonDisabled), for: UIControlState.disabled)
    }
    
    // 手机
    @IBAction func phoneNumDidChanged(_ sender: Any) {
        self.checkNextBtnStatus()
    }
    
    // 姓名
    @IBAction func nameDidChanged(_ sender: Any) {
        self.checkNextBtnStatus()
    }
    
    @IBAction func protocolBtnClick(_ sender: Any) {
        let webVC = KSWebViewController()
        webVC.url = "http://id.51myd.com/agreement"
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    @IBAction func agreeBtnClick(_ sender: Any) {
        agreeBtn.isSelected = !agreeBtn.isSelected
        
        self.checkNextBtnStatus()
    }
    
    private func checkNextBtnStatus() {
        if (self.phoneNumTf.text?.isEmpty)! || (self.nameTf.text?.isEmpty)! || !self.agreeBtn.isSelected {
            self.nextBtn.isEnabled = false
        } else {
            self.nextBtn.isEnabled = true
        }
    }
    
    @IBAction func nextBtnClick(_ sender: Any) {
        if KSTools.checkPhoneNum(number: self.phoneNumTf.text!) {
            self.startLoading()
            request.requestCheckRegister(mobile: self.phoneNumTf.text!)
        } else {
            progressHUD.showMessage(text: "请输入正确的手机号", duration: 2, animated: true)
        }
    }
    
    @IBAction func loginBtnClick(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    /// MARK: - KSRequestDelegate
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        // 判断手机号是否注册过接口
        if apiType == KSApiType.KSCheckRegister {
            let registerModel = KSCheckRegisterModel.deserialize(from: result)
            if registerModel?.data?.isReginster == "1" {
                progressHUD.showMessage(text: "手机号已存在", duration: 2, animated: true)
            } else {
                if (self.refereeAccountTf.text?.isEmpty)! {
                    self.skipNextPage(exist: false)
                } else {
                    request.requestGetInvite(inviteUser: self.refereeAccountTf.text!)
                }
            }
        } else if apiType == KSApiType.KSGetInvite {
            self.skipNextPage(exist: true)
        }
    }
    
    private func skipNextPage(exist: Bool) {
        let nextPage = KSFinishRegisterViewController()
        if exist {
            nextPage.userInfo = ["mobile": self.phoneNumTf.text!, "real_name": self.nameTf.text!, "invite_user": self.refereeAccountTf.text!]
        } else {
            nextPage.userInfo = ["mobile": self.phoneNumTf.text!, "real_name": self.nameTf.text!]
        }
        
        self.navigationController?.pushViewController(nextPage, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.phoneNumTf.resignFirstResponder()
        self.nameTf.resignFirstResponder()
        self.refereeAccountTf.resignFirstResponder()
    }
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
